package cn.iocoder.yudao.module.psy.controller.admin.wxuser;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.translate.core.TranslateUtils;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.enums.common.SexEnum;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.psy.controller.admin.wxuser.vo.*;
import cn.iocoder.yudao.module.psy.dal.dataobject.wxuser.WxUserDO;
import cn.iocoder.yudao.module.psy.service.wxuser.WxUserService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 学生信息")
@RestController
@RequestMapping("/psy/wx-user")
@Validated
public class WxUserController {

    @Resource
    private WxUserService wxUserService;

    @PostMapping("/create")
    @Operation(summary = "创建学生信息")
    @PreAuthorize("@ss.hasPermission('psy:wx-user:create')")
    public CommonResult<Long> createWxUser(@Valid @RequestBody WxUserSaveReqVO createReqVO) {
        return success(wxUserService.createWxUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新学生信息")
    @PreAuthorize("@ss.hasPermission('psy:wx-user:update')")
    public CommonResult<Boolean> updateWxUser(@Valid @RequestBody WxUserSaveReqVO updateReqVO) {
        wxUserService.updateWxUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除学生信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('psy:wx-user:delete')")
    public CommonResult<Boolean> deleteWxUser(@RequestParam("id") Long id) {
        wxUserService.deleteWxUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得学生信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('psy:wx-user:query')")
    public CommonResult<WxUserRespVO> getWxUser(@RequestParam("id") Long id) {
        WxUserDO wxUser = wxUserService.getWxUser(id);
        return success(BeanUtils.toBean(wxUser, WxUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得学生信息分页")
    @PreAuthorize("@ss.hasPermission('psy:wx-user:query')")
    public CommonResult<PageResult<WxUserRespVO>> getWxUserPage(@Valid WxUserPageReqVO pageReqVO) {
        PageResult<WxUserDO> pageResult = wxUserService.getWxUserPage(pageReqVO);

        return success(BeanUtils.toBean(pageResult, WxUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出学生信息 Excel")
    @PreAuthorize("@ss.hasPermission('psy:wx-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWxUserExcel(@Valid WxUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);

        List<WxUserDO> list = wxUserService.getWxUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "学生信息.xls", "数据", WxUserRespVO.class,
                TranslateUtils.translate(BeanUtils.toBean(list, WxUserRespVO.class)) );
    }
    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入用户模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<WxUserImportExcelVO> list = Arrays.asList(

        );
        // 输出
        ExcelUtils.write(response, "用户导入模板.xls", "用户列表", WxUserImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入用户")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('system:user:import')")
    public CommonResult<UserImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<WxUserImportExcelVO> list = ExcelUtils.read(file, WxUserImportExcelVO.class);

        return success(wxUserService.importUserList(list, updateSupport));
    }


}